﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using DDCMS.Domain.Manager;
using Microsoft.AspNetCore.Identity;
using DDCMS.Models;

// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace DDCMS.Controllers
{
    public class LoginController : Controller
    {
        private readonly ILogger<LoginController> _logger;
        private readonly UserManager<User> _userManager;
        private readonly SignInManager<User> _signInManager;
        private readonly RoleManager<Role> _roleManager;
        public LoginController(ILogger<LoginController> logger,
            UserManager<User> userManager,
            SignInManager<User> signInManager,
            RoleManager<Role> roleManager)
        {
            this._logger = logger;
            _userManager = userManager;
            _roleManager = roleManager;
            _signInManager = signInManager;
           
        }
        // GET: /<controller>/
        public IActionResult Index()
        {
           
            return View();
        }
       
        public IActionResult Login(LoginViewModel obj)
        {
            if (ModelState.IsValid)
            {
                var result = _signInManager.PasswordSignInAsync(obj.UserName,obj.Password,obj.RememberMe,false).Result;
               
                if (result.Succeeded)
                {
                    return Json("success");
                }
                return Json("error");
            }
            return Json("error");
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult LoginOff()
        {
            _signInManager.SignOutAsync().Wait();
            return RedirectToAction("Index", "Login");
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <returns></returns>
        public IActionResult Register()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Register(RegisterViewModel obj)
        {
            if (ModelState.IsValid)
            {
                User user = new Domain.Manager.User();
                user.UserName = obj.UserName;
                user.Name = obj.FullName;
                user.Email = obj.Email;
                IdentityResult result = _userManager.CreateAsync(user, obj.Password).Result;
                if (result.Succeeded)
                {
                    if (!_roleManager.RoleExistsAsync("NormalUser").Result)
                    {
                        Role role = new Role();
                        role.Name = "NormalUser";
                        role.Type = 1;
                        IdentityResult roleResult = _roleManager.CreateAsync(role).Result;
                        if (!roleResult.Succeeded)
                        {
                            ModelState.AddModelError("","创建角色失败");
                            return View(obj);
                        }
                    }
                    _userManager.AddToRoleAsync(user, "NormalUser").Wait();
                    return RedirectToAction("Index","Login");
                }
            }
            return View(obj);
        }
    }
}
